package com.dkd.manage.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.dkd.common.constant.DkdContants;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.StringUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.domain.Node;
import com.dkd.manage.domain.VmType;
import com.dkd.manage.mapper.ChannelMapper;
import com.dkd.manage.mapper.NodeMapper;
import com.dkd.manage.mapper.VmTypeMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.mapper.VendingMachineMapper;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.service.IVendingMachineService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备管理Service业务层处理
 * 
 * @author lcl
 * @date 2024-10-28
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService 
{
    @Autowired
    private VendingMachineMapper vendingMachineMapper;

    @Autowired
    private VmTypeMapper vmTypeMapper;

    @Autowired
    private NodeMapper nodeMapper;

    @Autowired
    private ChannelMapper channelMapper;

    /**
     * 查询设备管理
     * 
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id)
    {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备管理列表
     * 
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine)
    {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertVendingMachine(VendingMachine vendingMachine)
    {
        vendingMachine.setCreateTime(DateUtils.getNowDate());
        // 自动生成设备编号
        String uuid = UUIDUtils.generateClientId("1-");
        String clientInnerCode = StringUtils.substring(uuid, 0, 8);
        vendingMachine.setInnerCode(clientInnerCode);

        // 根据设备类型id获取设备容量信息
        VmType vmType = vmTypeMapper.selectVmTypeById(vendingMachine.getVmTypeId());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());

        // 根据点位ID设置区域id、合作商id、详细地址、商圈类型
        Node node = nodeMapper.selectNodeByIdLong(vendingMachine.getNodeId());
        BeanUtils.copyProperties(node, vendingMachine, "id");
        vendingMachine.setRegionId((long) node.getRegionId());
        vendingMachine.setPartnerId((long) node.getPartnerId());
        vendingMachine.setBusinessType((long) node.getBusinessType());
        vendingMachine.setAddr(node.getAddress());

        // 上次补货时间
        vendingMachine.setLastSupplyTime(DateUtils.getNowDate());

        // 默认设备状态
        vendingMachine.setVmStatus(DkdContants.VM_STATUS_NODEPLOY); // 未投放

        // 根据货道行和列-生成货道数

        Long vmRow = vmType.getVmRow();
        Long vmCol = vmType.getVmCol();
        vendingMachineMapper.insertVendingMachine(vendingMachine);
        Long vmId = vendingMachine.getId();
        System.out.println("vmId before conversion: " + vmId);
        // 预定义channel的列表
        List<Channel> channelList = new ArrayList<>();

        for (int i = 1; i<=vmRow;  i++){
            for (int j = 1; j<=vmCol; j++){
                Channel channel = new Channel();
                String code = String.format("%d-%d",i,j);
                channel.setChannelCode(code);
                channel.setVmId(vmId);
                channel.setInnerCode(clientInnerCode);
                channel.setMaxCapacity(vmType.getChannelMaxCapacity());
                channel.setCurrentCapacity(vmType.getChannelMaxCapacity());
                channelList.add(channel);
            }
        }

        // 批量插入货道数据
        channelMapper.insertBatch(channelList);

        return 11;
    }

    /**
     * 修改设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine)
    {
        vendingMachine.setUpdateTime(DateUtils.getNowDate());

        // 根据设备类型id获取设备容量信息
        VmType vmType = vmTypeMapper.selectVmTypeById(vendingMachine.getVmTypeId());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());

        // 根据点位ID设置区域id、合作商id、详细地址、商圈类型
        Node node = nodeMapper.selectNodeByIdLong(vendingMachine.getNodeId());
        BeanUtils.copyProperties(node, vendingMachine, "id");
        vendingMachine.setAddr(node.getAddress());

        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备管理
     * 
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids)
    {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备管理信息
     * 
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id)
    {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }
}
